<?php
session_start();

// 检查是否已经配置
if (file_exists('config.php')) {
    include 'config.php';
    if ($_SESSION['logged_in']) {
        header('Location: admin.php');
        exit;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!file_exists('config.php')) {
        // 配置网站名称和密码
        $website_name = filter_input(INPUT_POST, 'website_name', FILTER_SANITIZE_STRING);
        $password = password_hash(filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING), PASSWORD_DEFAULT);

        // 使用heredoc语法生成配置文件内容
        $config_content = <<<EOT
<?php
\$website_name = '$website_name';
\$password = '$password';
EOT;

        file_put_contents('config.php', $config_content);
        echo "مۇۋەپپەقىيەتلىك بولدى،مىمانى ئۇنتۇپ قالسىڭىز بۇ ھۆججەتنى يۇيۋېتىڭ config.php";
        echo '<meta http-equiv="refresh" content="3;url=login.php">';
        exit;
    } else {
        // 登录
        include 'config.php';
        $input_password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
        if (password_verify($input_password, $password)) {
            $_SESSION['logged_in'] = true;
            header('Location: admin.php');
            exit;
        } else {
            echo "شىفىر خاتا！";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录kirix</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="container">
        <?php if (!file_exists('config.php')): ?>
            <h1>ئىسىم قويۇڭ</h1>
            <form method="post">
                <label for="website_name">نامى：名称</label>
                <input type="text" id="website_name" style="border: 2px solid #007BFF; border-radius: 8px;" name="website_name" required><br><br>
                <label for="password">شېفىر：密码</label>
                <input type="password" id="password" style="border: 2px solid #007BFF; border-radius: 8px;" name="password" required><br><br>
                <input type="submit" class="button" value="配置قاچىلا">
            </form>
        <?php else: ?>
            <h1>ئارقا سۇپىغا كىرىش</h1>
            <form method="post">
                <label for="password">مەخپى نومۇر：</label>
                <input type="password" id="password" style="border: 2px solid #007BFF; border-radius: 8px;" name="password" required><br><br>
                <input type="submit" class="button" value="كىرىش进入">
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
